// store index.js
import Vue from 'vue'
import Vuex from 'vuex'
import da from "element-ui/src/locale/lang/da";

Vue.use(Vuex)
// 初始化时用sessionStore.getItem('token'),这样子刷新页面就无需重新登录
const state = {
  email:window.sessionStorage.getItem('email'),
  username: window.sessionStorage.getItem('username'),
  token: window.sessionStorage.getItem('token'),
  id: window.sessionStorage.getItem('id')
};
const mutations = {
  //将token保存到sessionStorage里，token表示登陆状态
  //这里的state不占用参数位置，不用传入state，是全局的
  SET_TOKEN: (state,data) => {
    state.token = data
    window.sessionStorage.setItem('token', data)
  },
  //获取用户名
  GET_USERNAME: (state,data) => {
    // 把用户名存起来
    state.username = data
    window.sessionStorage.setItem('username', data)
  },
  SET_ID:(state,data) => {
    state.id = data
    window.sessionStorage.setItem('id', data)
  },
  SET_EMAIL:(state,data)=>{
    state.email =data,
      window.sessionStorage.setItem('email', data)
  },
  //退出
  LOGOUT: (state) => {
    // 退出的时候要清除token
    state.email = null
    state.token = null
    state.id = null
    state.username = null
    window.sessionStorage.removeItem('token')
    window.sessionStorage.removeItem('id')
    window.sessionStorage.removeItem('username')
    window.sessionStorage.removeItem('email')
  }
}

const actions = {
}
export default new Vuex.Store({
  state,
  mutations,
  actions
})
